package com.cn.service.GoodsService;

import com.cn.Mapper.goodsMapper.CartMapper;
import com.cn.entity.goods.Goods_cart;
import com.cn.util.SqlsesstionUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

/**
 * @ClassName CartService
 * @Description TODO
 * @Author yujiajing
 * @Date 2022/10/11 21:16
 * @Version 1.0
 */


/**
 * (手动填入信息)加入购物车服务
 *
 */
@Service
public class CartService {
    public void InsertCart(String goods_id, String goods_name, Integer goods_price, Integer goods_number, Integer goods_subtotal){
        SqlSession sqlSession= SqlsesstionUtil.getSqlSession();
        CartMapper cartMapper=sqlSession.getMapper(CartMapper.class);
        Goods_cart goods_cart=new Goods_cart();
        cartMapper.InsertCart(goods_cart);
    }

    /**
     * 购物车灵魂
     * (根据商品表的商品id)加入购物车服务
     *
     */
    public void InsertCartByAuto(String goods_id){
        SqlSession sqlSession= SqlsesstionUtil.getSqlSession();
        CartMapper cartMapper=sqlSession.getMapper(CartMapper.class);
        cartMapper.InsertCartById(goods_id);
        cartMapper.updateCartByIdForNumber(1,goods_id);
    }


    /**
     * 根据商品id让某个商品数目+1
     **
     */
    public void autoAddOneForNumber(String goods_id){

        SqlSession sqlSession= SqlsesstionUtil.getSqlSession();
        CartMapper cartMapper=sqlSession.getMapper(CartMapper.class);
        cartMapper.autoAddOne(goods_id);

    }

    /**
     * 自动更新小计
     */
    public void analyseSubtotal(String goods_id){

        SqlSession sqlSession= SqlsesstionUtil.getSqlSession();
        CartMapper cartMapper=sqlSession.getMapper(CartMapper.class);
        cartMapper.analyseSubtotal(goods_id);

    }

    /**
     * 退掉一行订单
     */
    public void deleteCartById(String goods_id){
        SqlSession sqlSession= SqlsesstionUtil.getSqlSession();
        CartMapper cartMapper=sqlSession.getMapper(CartMapper.class);
        cartMapper.deleteCartById(goods_id);
    }

    /**
     * 清空购物车服务
     */
    public void EmptyCart(){
        SqlSession sqlSession= SqlsesstionUtil.getSqlSession();
        CartMapper cartMapper=sqlSession.getMapper(CartMapper.class);
        cartMapper.emptyCart();
    }
}
